package com.sias.bootdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sias.bootdemo.common.Code;
import com.sias.bootdemo.common.Result;
import com.sias.bootdemo.pojo.Book;
import com.sias.bootdemo.pojo.Brand;
import com.sias.bootdemo.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @PostMapping("/getPage")
    public Result getAll(@RequestBody Brand brand) {
        IPage page = new Page(brand.getCurrentPage(), brand.getPageSize());
        QueryWrapper<Brand> brandQueryWrapper = new QueryWrapper<>();
        brandQueryWrapper.like(brand.getBrandName() != null && !brand.getBrandName().equals(""), "brand_name", brand.getBrandName());
        brandQueryWrapper.like(brand.getCompanyName() != null && !brand.getCompanyName().equals(""), "company_name", brand.getCompanyName());
        brandQueryWrapper.eq(brand.getStatus() != null, "status", brand.getStatus());
        brandService.page(page, brandQueryWrapper);
        return new Result(Code.GET_OK, page, "");
    }

    @DeleteMapping("/deleteBachIds")
    public Result deleteBach(@RequestBody List<Long> ids) {
        boolean b = brandService.removeByIds(ids);
        return new Result(b ? Code.DELETE_OK : Code.DELETE_ERR, b, "");
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean b = brandService.removeById(id);
        return new Result(b ? Code.DELETE_OK : Code.DELETE_ERR, b, "");
    }
    @PostMapping("/add")
    public Result add(@RequestBody Brand brand) {
        boolean b = brandService.save(brand);
        return new Result(b ? Code.SAVE_OK : Code.SAVE_ERR, b, "");

    }
}
